<script setup name="name">
import { productionPlanListAPI } from '@/api/order/subcontracting/order.js'
const { proxy } = getCurrentInstance()

const props = defineProps({
  isShow: {
    type: Boolean,
    default: false
  }
})
// 物料类别
// const { sys_material_type } = proxy.useDict('sys_material_type')

const emit = defineEmits(['selectsubmit', 'preservation', 'cancel', 'reset'])

const loading = ref(false)

// 查询条件
const query = reactive({
  pageNum: 1,
  pageSize: 10,
  planMaterialName: '', // 名称
  planAssociatedCode: '' // 编码
})

const handleSelect = ref({ id: '' })
const TableData = ref([])
const total = ref(0)

// 勾选数据
const handleSelectionChange = row => {
  handleSelect.value = row
}

// 是否禁用 保存
const d = computed(() => {
  return handleSelect.value?.planId ? false : true
})

// 提交
const preservation = () => {
  const Obj = handleSelect.value
  emit('preservation', Obj)
}

// 重置
const reset = () => {
  for (let key in query) {
    query[key] = ''
  }
  query.bomType = props.bomType
  getList()
}

// 查询 Bom
const getList = async () => {
  query.bomType = props.bomType
  try {
    loading.value = true
    const res = await productionPlanListAPI(query)
    total.value = res.total
    TableData.value = res.rows
  } catch {
  } finally {
    loading.value = false
  }
}
// getList()
</script>
<template>
  <div class="app-container">
    <p-XiaoheDialog :dialogVisible="props.isShow" title="关联单号" @close="emit('cancel')" width="50%">
      <template #body>
        <!-- 查询条件 -->
        <el-form :inline="true" label-width="100px">
          <el-form-item label="生产计划单号">
            <el-input clearable v-model.trim="query.planAssociatedCode" placeholder="输入生产计划单号" />
          </el-form-item>
          <el-form-item label="物料编码">
            <el-input clearable v-model.trim="query.planMaterialCode" placeholder="输入物料编码" />
          </el-form-item>
        </el-form>
        <el-divider />
        <el-table
          stripe
          border
          :loading="loading"
          v-if="!loading"
          :data="TableData"
          highlight-current-row
          @current-change="handleSelectionChange"
        >
          <el-table-column label="序号" width="80" type="index" />
          <el-table-column width="80">
            <template #default="scope">
              <el-radio-group v-model="handleSelect.planId">
                <el-radio :label="scope.row.planId || ''" size="large">&nbsp;</el-radio>
              </el-radio-group>
            </template>
          </el-table-column>
          <el-table-column label="生产计划单号" prop="planCode" width="auto" />
          <el-table-column label="物料名称" prop="planMaterialName" width="auto" />
          <el-table-column label="物料编码" prop="planMaterialCode" width="auto" />
          <el-table-column label="物料规格" prop="planMaterialSpec" width="auto" />
        </el-table>
        <div class="pagination">
          <pagination
            v-show="total > 0"
            :total="total"
            v-model:page="query.pageNum"
            v-model:limit="query.pageSize"
            @pagination="getList"
          />
        </div>
        <!-- 按钮 -->
        <div>
          <el-row class="Btn">
            <el-button type="primary" @click="getList">查询列表</el-button>
            <el-button type="info" @click="reset">重置</el-button>
            <el-button type="success" @click="preservation" :disabled="d">保存</el-button>
            <el-button type="info" @click="emit('cancel')">取消</el-button>
          </el-row>
        </div>
      </template>
    </p-XiaoheDialog>
  </div>
</template>

<style scoped lang="scss">
.Btn {
  margin-top: 10px;
  float: right;
}
.app-container {
  width: 100%;
}
</style>
